package com.xj.api.bussiness.deviceLocation.web;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xj.api.base.web.BaseController;
import com.xj.api.bussiness.deviceLocation.service.ITbDeviceLocationService;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.bussiness.deviceLocation.entity.TbDeviceLocation;
import com.xj.common.bussiness.deviceState.entity.TbDeviceState;

import io.swagger.annotations.ApiOperation;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author Mars
 * @since 2017-08-24
 */
@RestController
@RequestMapping("deviceLocation")
public class TbDeviceLocationController extends BaseController{
	
	@Autowired
	private ITbDeviceLocationService deviceLocationService;
	
	@GetMapping("/list.auth")
	@ApiOperation(value = "  列表", httpMethod = "GET", notes = "  列表")
	public AbstractBean tbdevicelocationList(@RequestParam(required=false,defaultValue="0") Integer pageNum,@RequestParam(required=false,defaultValue="10") Integer pageSize,@RequestParam String mac){
		return json(deviceLocationService.selectMapsPage(new Page<TbDeviceLocation>(pageNum, pageSize), new EntityWrapper<TbDeviceLocation>().eq("mac", mac)));
	}

	@GetMapping("/{mac}")
	@ApiOperation(value = "获取位置詳情", httpMethod = "GET", notes = "获取位置詳情")
	public AbstractBean getLastJpg(@PathVariable String mac){
		return json(deviceLocationService.selectOne(new EntityWrapper<TbDeviceLocation>().eq("device_mac", mac).orderBy("create_time", false).last("limit 1")));
	
	}
	
	
	@SuppressWarnings("unchecked")
	@GetMapping("/{mac}.auth")
	@ApiOperation(value = "获取单点詳情", httpMethod = "GET", notes = "根据设备mac（mac）和查询的开始时间（beginTime）和结束时间（endTime）获取单点詳情")
	public AbstractBean getJpg(@PathVariable String mac,@PathVariable Date beginTime ,@PathVariable Date endTime){
		return json(deviceLocationService.selectObj(Condition.create().eq("device_mac", mac).between("create_time", beginTime, endTime)));
	}
	
	@PostMapping("/save")
	@ApiOperation(value = "  新增", httpMethod = "POST", notes = "定时增加位置信息mac不可为空")
	public AbstractBean add(TbDeviceLocation tbdevicelocation){
		if(tbdevicelocation.getDeviceMac().equals(null)&&tbdevicelocation.getDeviceMac().equals("")) {
			return fail("设备mac不能为空","-101");
			}
			if(!deviceLocationService.insert(tbdevicelocation)){
			return error();
		    }
			return success();
	}
	

}
